using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Soft2ApiDemo.Domain.Entity;
using Soft2ApiDemo.Domain.Repository;
using Soft2ApiDemo.Infrastructure.File;
using Soft2ApiDemo.Infrastructure.Utils;

namespace Soft2ApiDemo.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class FilesController : ControllerBase
    {
        private readonly IAppFileUpload _appFileUpload;

        public FilesController(IAppFileUpload appFileUpload)
        {
            _appFileUpload = appFileUpload;
        }

        [HttpPost]
        [Route("/files")]
        public async Task<string> OnPostUploadAsync(IFormCollection files)
        {
            var list = await _appFileUpload.UploadFile(files);
            if (list.Count() > 0)
            {
                return new
                {
                    Code = 1000,
                    Msg = "上传成功",
                    Data = list
                }.SerializeJson();
            }
            else
            {
                return new
                {
                    Code = 4000,
                    Msg = "上传失败，文件扩展名不被允许、文件大小不被允许",
                    Data = list
                }.SerializeJson();
            }

        }
    }
}